<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pdf预览</title>
<!-- <script src="//mozilla.github.io/pdf.js/build/pdf.js"></script> -->
<script src="js/pdf.js"></script>

<div style="margin: auto;width: 500px;">
  <a href="javascript:void(0)" id="prev">上一页</a>
  <a href="javascript:void(0)"  id="next">下一页</a>
  &nbsp; &nbsp;
  <span>页码: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
<div  style="margin: auto;width: 600px;">
<canvas style="width: 100%;" id="the-canvas"></canvas>
</div>
</head>
<body>
<script type="text/javascript">
var url = '/springMvc/page/aa.pdf';
PDFJS.workerSrc = 'js/pdf.worker.js';

var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 1,
canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
function renderPage(num) {
pageRendering = true;
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
 var viewport = page.getViewport(scale);
 canvas.height = viewport.height;
 canvas.width = viewport.width;

 // Render PDF page into canvas context
 var renderContext = {
   canvasContext: ctx,
   viewport: viewport
 };
 var renderTask = page.render(renderContext);

 // Wait for rendering to finish
 renderTask.promise.then(function() {
   pageRendering = false;
   if (pageNumPending !== null) {
     // New page rendering is pending
     renderPage(pageNumPending);
     pageNumPending = null;
   }
 });
});

// Update page counters
document.getElementById('page_num').textContent = pageNum;
}

/**
* If another page rendering in progress, waits until the rendering is
* finised. Otherwise, executes rendering immediately.
*/
function queueRenderPage(num) {
if (pageRendering) {
 pageNumPending = num;
} else {
 renderPage(num);
}
}

/**
* Displays previous page.
*/
function onPrevPage() {
if (pageNum <= 1) {
 return;
}
pageNum--;
queueRenderPage(pageNum);
}
document.getElementById('prev').addEventListener('click', onPrevPage);

/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
 return;
}
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);

/**
* Asynchronously downloads PDF.
*/
PDFJS.getDocument(url).then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;

// Initial/first page rendering
renderPage(pageNum);
});
</script>
</body>
</html>